//
//  SmallDecisionsMhViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/20.
//

#import "SmallDecisionsMhViewController.h"
#import "SmallDecisionsMhCell.h"
#import "SmallDecisionsMhEditViewController.h"

@interface SmallDecisionsMhViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *bottomContainerView;
@property (nonatomic, strong) AlignButton *refreshBtn;
@property (nonatomic, strong) NSMutableArray *mhList;

@end

@implementation SmallDecisionsMhViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"猜盲盒";
    
    [self setDefaultMhData];
    [self generateRandomMh];
    [self bottomContainerView];
    [self refreshBtn];
    
}

- (void)setDefaultMhData {
    for(NSInteger i = 0,n = self.mhCount; i < n; i ++) {
        [self.mhList addObject:[NSString stringWithFormat:@"玩家%ld",i + 1]];
    }
}

- (void)generateRandomMh {
    int len = (int)self.mhList.count;
    while (len > 0) {
        int randomIndex = arc4random_uniform(len);
        [self.mhList exchangeObjectAtIndex:len - 1 withObjectAtIndex:randomIndex];
        len --;
    }
    [self.collectionView reloadData];
}

#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.mhList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SmallDecisionsMhCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallDecisionsMhCellId" forIndexPath:indexPath];
    cell.mhImageView.hidden = NO;
    cell.mhLabel.hidden = YES;
    cell.mhLabel.text = self.mhList[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SmallDecisionsMhCell *cell = (SmallDecisionsMhCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.mhImageView.hidden = YES;
    cell.mhLabel.hidden = NO;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemW = (self.view.width - 10 * 5 ) / 3.0;
        layout.sectionInset = UIEdgeInsetsMake(5, 2, 5, 2);
        layout.itemSize = CGSizeMake(itemW, itemW);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.view addSubview:_collectionView];
        [_collectionView registerClass:[SmallDecisionsMhCell class] forCellWithReuseIdentifier:@"SmallDecisionsMhCellId"];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(10);
            make.left.right.top.mas_offset(0);
            make.bottom.equalTo(self.bottomContainerView.mas_top);
        }];
    }
    
    return _collectionView;
}

- (UIView *)bottomContainerView {
    if(!_bottomContainerView) {
        _bottomContainerView = [[UIView alloc] init];
        [self.view addSubview:_bottomContainerView];
        
        [_bottomContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_offset(0);
        }];
    }
    return _bottomContainerView;
}

- (AlignButton *)refreshBtn {
    if(!_refreshBtn) {
        _refreshBtn = [AlignButton buttonWithType:UIButtonTypeCustom];
        [_refreshBtn setTitle:@"再来一局" forState:UIControlStateNormal];
        [_refreshBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        [_refreshBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        _refreshBtn.alignType = AlignType_TextRight;
        _refreshBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(14);
        _refreshBtn.layer.cornerRadius = 25;
        _refreshBtn.layer.masksToBounds = YES;
        _refreshBtn.backgroundColor = SmallDecisionsThemeColor;
        [_refreshBtn addTarget:self action:@selector(refreshBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomContainerView addSubview:_refreshBtn];
        [_refreshBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(50);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.top.mas_offset(30);
            make.bottom.mas_offset(-30);
        }];
    }
    return _refreshBtn;
}

- (NSMutableArray *)mhList {
    if(!_mhList) {
        _mhList = [[NSMutableArray alloc] init];
    }
    return _mhList;
}

- (void)refreshBtnAction {
    [self generateRandomMh];
}

- (UIView *)setRightBarButton {
    UIButton *addItemsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addItemsBtn setTitle:@"编辑" forState:UIControlStateNormal];
    addItemsBtn.titleLabel.font = SmallDecisionsSetFontSize(13);
    [addItemsBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
    addItemsBtn.frame = CGRectMake(0, 0, 30, 30);
    return addItemsBtn;
}

- (void)rightBarButtonAction:(UIButton *)view {
    SmallDecisionsMhEditViewController *mhEditVC = [[SmallDecisionsMhEditViewController alloc] init];
    mhEditVC.list = self.mhList;
    weakSelf(self);
    mhEditVC.callback = ^(NSArray *names) {
        if (names.count > 0) {
            [weakself.mhList removeAllObjects];
            [weakself.mhList addObjectsFromArray:names];
            [weakself generateRandomMh];
        }
    };
    [SmallDecisionsNavigatorManager sysPresentViewController:mhEditVC];
}

@end
